home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 741 / rkrm_lib1 / rkrm_lib1.lha / Intuition / Menus / simplemenu.c < prev   
C/C++ Source or Header  |  1992-09-03  |  8KB  |  256 lines

  1. ;/* simplemenu.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 simplemenu.c
  3. Blink FROM LIB:c.o,simplemenu.o TO simplemenu LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. /*
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. /*
  34. ** simplemenu.c: how to use the menu system with a window under all OS versions.
  35. */
  36. #define INTUI_V36_NAMES_ONLY
  37.  
  38. #include <exec/types.h>
  39. #include <exec/memory.h>
  40. #include <graphics/text.h>
  41. #include <intuition/intuition.h>
  42. #include <intuition/intuitionbase.h>
  43.  
  44. #include <clib/exec_protos.h>
  45. #include <clib/graphics_protos.h>
  46. #include <clib/intuition_protos.h>
  47.  
  48. #include <stdio.h>
  49. #include <string.h>
  50.  
  51. #ifdef LATTICE
  52. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  53. int chkabort(void) { return(0); }  /* really */
  54. #endif
  55.  
  56. /*  These values are based on the ROM font Topaz8. Adjust these  */
  57. /*  values to correctly handle the screen's current font.        */
  58. #define MENWIDTH  (56+8)  /* Longest menu item name * font width */
  59.                           /* + 8 pixels for trim                 */
  60. #define MENHEIGHT (10)    /* Font height + 2 pixels              */
  61.  
  62. struct Library *GfxBase;
  63. struct Library *IntuitionBase;
  64.  
  65. /* To keep this example simple, we'll hard-code the font used for menu */
  66. /* items.  Algorithmic layout can be used to handle arbitrary fonts.   */
  67. /* Under Release 2, GadTools provides font-sensitive menu layout.      */
  68. /* Note that we still must handle fonts for the menu headers.          */
  69. struct TextAttr Topaz80 =
  70. {
  71.     "topaz.font", 8, 0, 0
  72. };
  73.  
  74. struct IntuiText menuIText[] =
  75. {
  76.     { 0, 1, JAM2, 0, 1, &Topaz80, "Open...",  NULL },
  77.     { 0, 1, JAM2, 0, 1, &Topaz80, "Save",     NULL },
  78.     { 0, 1, JAM2, 0, 1, &Topaz80, "Print \273",  NULL },
  79.     { 0, 1, JAM2, 0, 1, &Topaz80, "Draft",    NULL },
  80.     { 0, 1, JAM2, 0, 1, &Topaz80, "NLQ",      NULL },
  81.     { 0, 1, JAM2, 0, 1, &Topaz80, "Quit",     NULL }
  82. };
  83.  
  84. struct MenuItem submenu1[] =
  85. {
  86.     { /* Draft  */
  87.     &submenu1[1], MENWIDTH-2,  -2 ,            MENWIDTH, MENHEIGHT,
  88.     ITEMTEXT | MENUTOGGLE | ITEMENABLED | HIGHCOMP,
  89.     0, (APTR)&menuIText[3], NULL, NULL, NULL, NULL
  90.     },
  91.     { /* NLQ    */
  92.     NULL,         MENWIDTH-2, MENHEIGHT-2, MENWIDTH, MENHEIGHT,
  93.     ITEMTEXT | MENUTOGGLE | ITEMENABLED | HIGHCOMP,
  94.     0, (APTR)&menuIText[4], NULL, NULL, NULL, NULL
  95.     }
  96. };
  97.  
  98. struct MenuItem menu1[] =
  99. {
  100.     { /* Open... */
  101.     &menu1[1], 0, 0,            MENWIDTH, MENHEIGHT,
  102.     ITEMTEXT | MENUTOGGLE | ITEMENABLED | HIGHCOMP,
  103.     0, (APTR)&menuIText[0], NULL, NULL, NULL, NULL
  104.     },
  105.     { /* Save    */
  106.     &menu1[2], 0,  MENHEIGHT ,  MENWIDTH, MENHEIGHT,
  107.     ITEMTEXT | MENUTOGGLE | ITEMENABLED | HIGHCOMP,
  108.     0, (APTR)&menuIText[1], NULL, NULL, NULL, NULL
  109.     },
  110.     { /* Print   */
  111.     &menu1[3], 0, 2*MENHEIGHT , MENWIDTH, MENHEIGHT,
  112.     ITEMTEXT | MENUTOGGLE | ITEMENABLED | HIGHCOMP,
  113.     0, (APTR)&menuIText[2], NULL, NULL, &submenu1[0] , NULL
  114.     },
  115.     { /* Quit    */
  116.     NULL, 0, 3*MENHEIGHT , MENWIDTH, MENHEIGHT,
  117.     ITEMTEXT | MENUTOGGLE | ITEMENABLED | HIGHCOMP,
  118.     0, (APTR)&menuIText[5], NULL, NULL, NULL, NULL
  119.     },
  120. };
  121.  
  122. /* We only use a single menu, but the code is generalizable to */
  123. /* more than one menu.                                         */
  124. #define NUM_MENUS 1
  125.  
  126. STRPTR menutitle[NUM_MENUS] =  {   "Project"   };
  127.  
  128. struct Menu menustrip[NUM_MENUS] =
  129. {
  130.     {
  131.     NULL,                    /* Next Menu          */
  132.     0, 0,                    /* LeftEdge, TopEdge, */
  133.     0, MENHEIGHT,            /* Width, Height,     */
  134.     MENUENABLED,             /* Flags              */
  135.     NULL,                    /* Title              */
  136.     &menu1[0]                /* First item         */
  137.     }
  138. };
  139.  
  140. struct NewWindow mynewWindow =
  141. {
  142. 40,40, 300,100, 0,1, IDCMP_CLOSEWINDOW | IDCMP_MENUPICK,
  143. WFLG_DRAGBAR | WFLG_ACTIVATE | WFLG_CLOSEGADGET, NULL,NULL,
  144. "Menu Test Window", NULL,NULL,0,0,0,0,WBENCHSCREEN
  145. };
  146.  
  147. /* our function prototypes */
  148. VOID handleWindow(struct Window *win, struct Menu *menuStrip);
  149.  
  150. /*      Main routine.         */
  151. /*                            */
  152. VOID main(int argc, char **argv)
  153. {
  154. struct Window *win=NULL;
  155. UWORD left, m;
  156.  
  157. /* Open the Graphics Library */
  158. GfxBase = OpenLibrary("graphics.library",33);
  159. if (GfxBase)
  160.     {
  161.     /* Open the Intuition Library */
  162.     IntuitionBase = OpenLibrary("intuition.library", 33);
  163.     if (IntuitionBase)
  164.         {
  165.         if ( win = OpenWindow(&mynewWindow) )
  166.             {
  167.             left = 2;
  168.             for (m = 0; m < NUM_MENUS; m++)
  169.                 {
  170.                 menustrip[m].LeftEdge = left;
  171.                 menustrip[m].MenuName = menutitle[m];
  172.                 menustrip[m].Width = TextLength(&win->WScreen->RastPort,
  173.                     menutitle[m], strlen(menutitle[m])) + 8;
  174.                 left += menustrip[m].Width;
  175.                 }
  176.             if (SetMenuStrip(win, menustrip))
  177.                 {
  178.                 handleWindow(win, menustrip);
  179.                 ClearMenuStrip(win);
  180.                 }
  181.             CloseWindow(win);
  182.             }
  183.         CloseLibrary(IntuitionBase);
  184.         }
  185.     CloseLibrary(GfxBase);
  186.     }
  187. }
  188.  
  189. /*
  190. **   Wait for the user to select the close gadget.
  191. */
  192. VOID handleWindow(struct Window *win, struct Menu *menuStrip)
  193. {
  194. struct IntuiMessage *msg;
  195. SHORT done;
  196. ULONG class;
  197. UWORD menuNumber;
  198. UWORD menuNum;
  199. UWORD itemNum;
  200. UWORD subNum;
  201. struct MenuItem *item;
  202.  
  203. done = FALSE;
  204. while (FALSE == done)
  205.     {
  206.     /* we only have one signal bit, so we do not have to check which
  207.     ** bit broke the Wait().
  208.     */
  209.     Wait(1L << win->UserPort->mp_SigBit);
  210.  
  211.     while ( (FALSE == done) &&
  212.             (msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
  213.         {
  214.         class = msg->Class;
  215.         if(class == IDCMP_MENUPICK)   menuNumber = msg->Code;
  216.  
  217.         switch (class)
  218.             {
  219.             case IDCMP_CLOSEWINDOW:
  220.                 done = TRUE;
  221.                 break;
  222.             case IDCMP_MENUPICK:
  223.                 while ((menuNumber != MENUNULL) && (!done))
  224.                     {
  225.                     item = ItemAddress(menuStrip, menuNumber);
  226.  
  227.                     /* process this item
  228.                     ** if there were no sub-items attached to that item,
  229.                     ** SubNumber will equal NOSUB.
  230.                     */
  231.                     menuNum = MENUNUM(menuNumber);
  232.                     itemNum = ITEMNUM(menuNumber);
  233.                     subNum  = SUBNUM(menuNumber);
  234.  
  235.                     /* Note that we are printing all values, even things
  236.                     ** like NOMENU, NOITEM and NOSUB.  An application should
  237.                     ** check for these cases.
  238.                     */
  239.                     printf("IDCMP_MENUPICK: menu %d, item %d, sub %d\n",
  240.                         menuNum, itemNum, subNum);
  241.  
  242.                     /* This one is the quit menu selection...
  243.                     ** stop if we get it, and don't process any more.
  244.                     */
  245.                     if ((menuNum == 0) && (itemNum == 4))
  246.                         done = TRUE;
  247.  
  248.                     menuNumber = item->NextSelect;
  249.                     }
  250.                 break;
  251.             }
  252.         ReplyMsg((struct Message *)msg);
  253.         }
  254.     }
  255. }
  256.